package com.market.manager.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.market.dao.ProductDao;
import com.market.domain.Picture;
import com.market.domain.Product;
import com.market.domain.ProductInfo;
import com.market.domain.PurchaseSchedule;
import com.market.manager.ProductManager;

@Service("productManager")
public class ProductManagerImpl extends
		GenericManagerImpl<ProductDao, Product, Long> implements ProductManager {

	@Autowired
	public void setDao(ProductDao productDao) {
		this.dao = productDao;
	}

	public Integer fetchInventory(Long productID) {
		return this.dao.findInventory(productID);
	}
	
	

	public List<Product> fetchActiveWeeklyProducts() {
		return this.dao.findActiveWeeklyProducts();
	}

	public void storeInventory(Product product) {
		this.dao.saveInventory(product);		
	}
	
	
	

	public void storePriceInfo(Product product) {
		this.dao.savePriceInfo(product);
	}

	public List<Product> fetchAllHaveInventory() {
		return this.dao.findAllHaveInventory();
	}
	
	public List<Product> fetchAllNotFreshHaveInventory() {
		return this.dao.findAllNotFreshHaveInventory();
	}

	public Product fetchOne(Long id) {
		Product product = this.dao.findById(id);
		product.setPictures(this.fetchPictures(product));
		product.setPurchaseSchedules(this.fetchPurchaseSchedules(product));
		product.setProductInfo(this.fetchProductInfo(product));
		product.setIsAssociatedLoaded(Boolean.TRUE);
		return product;

	}

	public Product fetchOne(Product product) {
		if (product != null && product.getIsNew() == false) {
			return this.fetchOne(product.getId());
		} else {
			return product;
		}
	}

	public List<PurchaseSchedule> fetchPurchaseSchedules(Product product) {
		return this.dao.findPurchaseSchedules(product);
	}
	
	public List<Picture> fetchPictures(Product product) {
		return this.dao.findPictures(product);
	}
	
	public ProductInfo fetchProductInfo(Product product){
		return this.dao.findProductInfo(product);
	}
	
	public List<Product> fetchAllOrganicProducts() {
		return this.dao.findAllOrganicProducts();
	}

	public List<Product> fetchAllTofuProducts() {
		return this.dao.findAllTofuProducts();
	}
}
